<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>三列五行表格</title>
    <link rel="stylesheet" />
    <style>
      table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
      }
      th,
      td {
        padding: 10px;
        text-align: left;
        border: 1px solid #ccc;
        background-color: #fff6f6;
      }
      button {
        background-color: rgb(252, 171, 155);
        border: 1px solid #999;
        width: 80px;
        height: 30px;
      }
      button:hover {
        background-color: rgb(255, 233, 233);
      }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th>
            <span><input type="checkbox" id="checkAll" /> 全选</span>
          </th>
          <th>第一列</th>
          <th>第二列</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
        <tr>
          <td><input type="checkbox" class="checkbox" /></td>
          <td>表单内容</td>
          <td>表单内容</td>
        </tr>
      </tbody>
    </table>
    <button>反选</button>

    <script>
      $(document).ready(function () {
        var $checkAll = $("#checkAll");
        var $checkbox = $(".checkbox");

        // 全选
        $checkAll.on("click", function () {
          $checkbox.prop("checked", $checkAll.prop("checked"));
        });

        // 小复选全勾 全选自动选中
        $checkbox.on("click", function () {
          $checkAll.prop(
            "checked",
            $checkbox.length === $(".checkbox:checked").length
          );
        });

        // 反选
        $("button").on("click", function () {
          $checkbox.each(function () {
            $(this).prop("checked", !$(this).prop("checked"));
          });
          $checkAll.prop(
            "checked",
            $checkbox.length === $(".checkbox:checked").length
          );
        });
      });
    </script>
  </body>
</html>
